package delivery.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import com.mysql.jdbc.PreparedStatement;

import delivery.bean.PermissaoBean;
import delivery.bean.UsuarioBean;
import delivery.connection.Conexao;

public class PermissaoDao extends Conexao {

	public boolean setPermissao(PermissaoBean permissaoBean) {
		return true;
	}

	public ArrayList<PermissaoBean> getPermissoesPorUsuario(
			UsuarioBean usuarioBean) {

		ArrayList<PermissaoBean> permissaoBean = new ArrayList<PermissaoBean>();
		String sql = "SELECT * FROM permissao AS p "
				+ "INNER JOIN lista_de_permissao AS lp ON p.id_permissao = lp.id_permissao "
				+ "WHERE lp.id_usuario = ? " + "ORDER BY  p.nome ASC ";

		try {
			PreparedStatement pS = (PreparedStatement) this.getConn().prepareStatement(sql);
			pS.setInt(1, usuarioBean.getId_usuario());
			ResultSet rS = pS.executeQuery();

			while (rS.next()) {
				PermissaoBean permissao = new PermissaoBean();
				ItemMenuDao itemMenuDao = new ItemMenuDao();

				permissao.setId_permissao(rS.getInt("id_permissao"));
				permissao.setNome(rS.getString("nome"));
				permissao.setItemMenu(itemMenuDao.getItemPorPermissao(rS.getInt("id_permissao")));
				permissaoBean.add(permissao);
			}
		} catch (SQLException erro) {
			JOptionPane.showMessageDialog(null, erro.getMessage());
		}

		return permissaoBean;
	}

}
